package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.report.AddReportRequest;
import com.springboot_gradrecruit.model.dto.report.PageReportRequest;
import com.springboot_gradrecruit.model.dto.report.UpdateReportRequest;
import com.springboot_gradrecruit.model.entity.Image;
import com.springboot_gradrecruit.model.entity.Report;
import com.springboot_gradrecruit.model.vo.UserVO;
import com.springboot_gradrecruit.model.vo.image.ImageVO;
import com.springboot_gradrecruit.model.vo.report.ReportVO;
import com.springboot_gradrecruit.service.ImageService;
import com.springboot_gradrecruit.service.ReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zr
 */
@Tag(name = "反馈举报相关接口")
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Resource
    private ReportService reportService;

    @Resource
    private ImageService imageService;

    //添加反馈举报
    @Operation(summary = "添加反馈举报")
    @PostMapping("/addReport")
    public R<Boolean> addReport(@RequestBody AddReportRequest addReportRequest){

        //1. 校验参数是否为空
        if(addReportRequest==null){
            throw new RuntimeException("参数为空");
        }

        //调用service完成添加
        Long aLong = reportService.addReport(addReportRequest);
        if(aLong>0){
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");
    }

    //查询举报反馈
    @Operation(summary = "查询举报反馈，通过举报人账号")
    @PostMapping("/queryReportByAccount")
    public R<ReportVO> queryReport(String reportPersonAccount){
        //1.参数校验
        if(reportPersonAccount==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Report> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("report_person_account", reportPersonAccount);
        Report one = reportService.getOne(queryWrapper);
        ReportVO reportVO = BeanUtil.copyProperties(one, ReportVO.class);
        return R.success(reportVO, "查询成功");
    }

    //查询举报反馈的图片
    @Operation(summary = "查询举报反馈的图片，通过类别ID")
    @PostMapping("/queryReportImage")
    public R<List<ImageVO>> queryReportImage(Long reportId){
        //1.参数校验
        if(reportId==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Image> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("image_name_id", reportId);
        queryWrapper.eq("image_name", "report");
        List<Image> listImage = imageService.list(queryWrapper);
        List<ImageVO> listImageVO = BeanUtil.copyToList(listImage, ImageVO.class);
        return R.success(listImageVO, "查询成功");
    }

    //修改举报反馈
    @Operation(summary = "修改举报反馈")
    @PostMapping("/updateReport")
    public R<Boolean> updateReport(@RequestBody UpdateReportRequest updateReportRequest){
        //1.参数校验
        if(updateReportRequest==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成修改
        boolean updateResult = reportService.updateReport(updateReportRequest);
        if (updateResult) {
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }


    //分页获取举报反馈
    @Operation(summary = "分页获取举报反馈")
    @PostMapping("/pageReport")
    public R<PageResponse<ReportVO>> pageReport(@RequestBody PageReportRequest pageReportRequest){
        //1.参数校验
        if(pageReportRequest==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        PageResponse<ReportVO> pageReport = reportService.pageReport(pageReportRequest);
        return R.success(pageReport, "查询成功");
    }


    @Operation(summary = "查询反馈举报-通过举报ID")
    @PostMapping("/queryReportByReportId")
    public R<ReportVO> queryReportByReportId(Long reportId){
        //1.参数校验
        if(reportId==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Report> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("report_id", reportId);
        Report one = reportService.getOne(queryWrapper);
        ReportVO reportVO = BeanUtil.copyProperties(one, ReportVO.class);
        return R.success(reportVO, "查询成功");
    }



}
